<template>
  <!-- 添加字典 -->
  <div class="dialog-container">
    <el-dialog
      :title="dialogType=='add'?'添加字典':dialogType == 'detail'?'字典信息':'修改字典'"
      :visible="dialogVisible"
      @open="initData"
      @close="closeDialog"
    >
      <el-form
        ref="form"
        :model="dataForm"
        class="dialog-form"
        :rules="rules"
        label-width="110px"
        size="mini"
        :disabled="dialogType == 'detail'"
      >
        <el-form-item label="码类型名称" prop="codeTypeName">
          <el-input v-model="dataForm.codeTypeName" placeholder="请输入码类型名称" maxlength="255" readonly />
        </el-form-item>
        <el-form-item label="码名称" prop="codeName">
          <el-input v-model="dataForm.codeName" type="textarea" :rows="4" placeholder="请输入码名称" maxlength="255" />
        </el-form-item>
        <el-form-item label="码类型" prop="codeType">
          <el-input v-model="dataForm.codeType" placeholder="请输入码类型" maxlength="255" readonly />
        </el-form-item>
        <el-form-item label="码" prop="code">
          <el-input v-model="dataForm.code" placeholder="请输入码" maxlength="255" />
        </el-form-item>
        <el-form-item label="排序" prop="sorting">
          <el-input-number v-model="dataForm.sorting" :max="999" :controls="false" :precision="0" type="number" placeholder="请输入字典排序" />
        </el-form-item>
      </el-form>
      <span v-if="dialogType !== 'detail'" slot="footer" class="dialog-footer">
        <el-button :loading="saveLoading" class="sure-btn" size="mini" type="primary" @click="handleSave">保存</el-button>
        <el-button size="mini" class="cancel-btn" @click="closeDialog">取消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, edit } from '@/api/system/dict'
export default {
  name: 'AddDictDetailFormDialog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    dialogType: {
      type: String,
      default: 'add'
    },
    obj: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      saveLoading: false,
      dataForm: {
        id: '',
        codeType: '',
        codeTypeName: '',
        codeName: '',
        code: '',
        sorting: 0
      },
      rules: {
        codeTypeName: [{ required: true, message: '请输入码类型名称', trigger: 'blur' }],
        codeName: [{ required: true, message: '请输入码名称', trigger: 'blur' }],
        codeType: [{ required: true, message: '请输入码类型', trigger: 'blur' }],
        code: [{ required: true, message: '请输入码', trigger: 'blur' }],
        sorting: [{ required: true, message: '请输入排序', trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  methods: {
    initData() {
      this.saveLoading = false
      if (this.obj) {
        this.dataForm = JSON.parse(JSON.stringify(this.obj))
      }
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    handleSave() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.saveLoading = true
          if (this.dataForm.id) {
            edit(this.dataForm).then(() => {
              this.$message({
                type: 'success',
                message: '保存成功'
              })
              this.$emit('flushPage')
              this.$emit('closeDialog')
              this.clearData()
            }).catch(() => {
              this.saveLoading = false
            })
          } else {
            console.log(this.dataForm)
            this.dataForm.dict = { id: this.dictId }
            add(this.dataForm).then(() => {
              this.$message({
                type: 'success',
                message: '保存成功'
              })
              this.$emit('flushPage')
              this.$emit('closeDialog')
              this.clearData()
            }).catch(() => {
              this.saveLoading = false
            })
          }
        }
      })
    },
    closeDialog() {
      this.$emit('closeDialog')
      this.clearData()
      this.$refs['form'].clearValidate()
    },
    clearData() {
      this.dataForm = {
        id: '',
        codeType: '',
        codeTypeName: '',
        codeName: '',
        code: '',
        sorting: 0
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
